/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License Version
 *  3 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 *  USA.
 */

#include <iostream>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QStringList>
#include <QtGui/QApplication>

#include <stdlib.h>

#include "connector.h"

static bool wantsToQuit;
static QString host = "127.0.0.1";
static qint16 port = 51999;

static Connector optra;

static bool sendcommand(const QString& command)
{
    if (command.trimmed().compare(QLatin1String("quit")) == 0){
        exit(EXIT_SUCCESS);
    }else
        return optra.send(command);

    return false;
}


static void interactive()
{

    QTextStream qin(stdin, QFile::ReadOnly);
    const char *qscript_prompt = "optra>";    
    const char *prompt = qscript_prompt;

    QString code;

    forever {
        QString line;

        printf("%s", prompt);
        fflush(stdout);

        line = qin.readLine();
        if (line.isNull())
            break;

        code += line;
        code += QLatin1Char('\n');

        if (line.trimmed().isEmpty()) {
            continue;

        } else if (!sendcommand(code)){
            exit(EXIT_FAILURE);
        } else {
            //sendcommand(code);
            // QScriptValue result = sendcommand(code);//, QLatin1String("typein"));

            code.clear();
            prompt = qscript_prompt;

            if (wantsToQuit)
                break;
        }
    }
}

int main(int argc, char** argv)
{
    QApplication *app;
    if (argc >= 2 && !qstrcmp(argv[1], "-tty")) {
        ++argv;
        --argc;
        app = new QApplication(argc, argv, QApplication::Tty);
    } else {
        app = new QApplication(argc, argv);
    }

    if (!optra.connectOptra(host, port)){
        QTextStream err(stderr, QFile::WriteOnly);
        err << QString("Can't connect to:%1 port:%2").arg(host).arg(port);
        return EXIT_FAILURE;
    }

    if (! *++argv) {
        interactive();
        return EXIT_SUCCESS;
    }

    while (const char *arg = *argv++) {
        QString fn = QString::fromLocal8Bit(arg);

        if (fn == QLatin1String("-i")) {
            interactive();
            break;
        }

        QString contents;
        int lineNumber = 1;

        if (fn == QLatin1String("-")) {
            QTextStream stream(stdin, QFile::ReadOnly);
            contents = stream.readAll();
        }

        else {
            QFile file(fn);

            if (file.open(QFile::ReadOnly)) {
                QTextStream stream(&file);
                contents = stream.readAll();
                file.close();

                // strip off #!/usr/bin/env qscript line
                if (contents.startsWith("#!")) {
                    contents.remove(0, contents.indexOf("\n"));
                    ++lineNumber;
                }
                if (!sendcommand(contents)){
                    return EXIT_FAILURE;
                }
            }
        }

        if (contents.isEmpty())
            continue;

    }
    delete app;

    return EXIT_SUCCESS;
}
